package com.yinhai.redis.cluster.model;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class ShardInfo implements Serializable{

	/**
	 * 
	 */
	private static final long serialVersionUID = 8947970231328481115L;
	
	private String name;
	private ServerInfo master;
	private List<ServerInfo> slaves=new ArrayList<ServerInfo>();
	private List<KeyRange> masterRange=new ArrayList<KeyRange>();
	private boolean enableSlave;
	
	private transient ShardInfo slave;
	private transient ShardInfo masterShard;
	
	/**
	 * standby的hash范围，当节点移除时
	 */
	private Set<KeyRange> standby=new HashSet<KeyRange>();
	
	public ServerInfo getMaster() {
		return master;
	}

	public void setMaster(ServerInfo master) {
		this.master = master;
	}

	public ShardInfo getMasterShard() {
		return masterShard;
	}

	public void setMasterShard(ShardInfo masterShard) {
		this.masterShard = masterShard;
	}

	public List<ServerInfo> getSlaves() {
		return slaves;
	}


	public void setSlaves(List<ServerInfo> slaves) {
		this.slaves = slaves;
	}
	
	public void addKeyRange(KeyRange key) {
		masterRange.add(key);
	}
	
	public void addStandby(KeyRange range){
		standby.add(range);
	}
	
	public void removeStandBy(KeyRange range){
		standby.remove(range);
	}

	public String getName() {
		return name;
	}


	public void setName(String name) {
		this.name = name;
	}
	
	public ShardInfo getSlave() {
		return slave;
	}

	public void setSlave(ShardInfo slave) {
		this.slave = slave;
		slave.setMasterShard(this);
	}

	
	public List<KeyRange> getMasterRange() {
		return masterRange;
	}

	public boolean isEnableSlave() {
		return enableSlave;
	}

	public void setEnableSlave(boolean enableSlave) {
		this.enableSlave = enableSlave;
	}



	public static class ServerInfo{
		private String host;
		private int port;
		public String getHost() {
			return host;
		}
		public void setHost(String host) {
			this.host = host;
		}
		public int getPort() {
			return port;
		}
		public void setPort(int port) {
			this.port = port;
		}
		@Override
		public int hashCode() {
			final int prime = 31;
			int result = 1;
			result = prime * result + ((host == null) ? 0 : host.hashCode());
			result = prime * result + port;
			return result;
		}
		@Override
		public boolean equals(Object obj) {
			if (this == obj)
				return true;
			if (obj == null)
				return false;
			if (getClass() != obj.getClass())
				return false;
			ServerInfo other = (ServerInfo) obj;
			if (host == null) {
				if (other.host != null)
					return false;
			} else if (!host.equals(other.host))
				return false;
			if (port != other.port)
				return false;
			return true;
		}
		@Override
		public String toString() {
			return "ServerInfo [host=" + host + ", port=" + port + "]";
		}
	}



	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + (enableSlave ? 1231 : 1237);
		result = prime * result + ((master == null) ? 0 : master.hashCode());
		result = prime * result
				+ ((masterRange == null) ? 0 : masterRange.hashCode());
		result = prime * result
				+ ((masterShard == null) ? 0 : masterShard.hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + ((slave == null) ? 0 : slave.hashCode());
		result = prime * result + ((slaves == null) ? 0 : slaves.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ShardInfo other = (ShardInfo) obj;
		if (enableSlave != other.enableSlave)
			return false;
		if (master == null) {
			if (other.master != null)
				return false;
		} else if (!master.equals(other.master))
			return false;
		if (masterRange == null) {
			if (other.masterRange != null)
				return false;
		} else if (!masterRange.equals(other.masterRange))
			return false;
		if (masterShard == null) {
			if (other.masterShard != null)
				return false;
		} else if (!masterShard.equals(other.masterShard))
			return false;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		if (slave == null) {
			if (other.slave != null)
				return false;
		} else if (!slave.equals(other.slave))
			return false;
		if (slaves == null) {
			if (other.slaves != null)
				return false;
		} else if (!slaves.equals(other.slaves))
			return false;
		return true;
	}

	@Override
	public String toString() {
		return "ShardInfo [name=" + name + ", master=" + master + ", slaves="
				+ slaves + ", masterRange=" + masterRange + ", enableSlave="
				+ enableSlave + ", slave=" + slave + ", masterShard="
				+ masterShard + "]";
	}

}
